import api from '@/api/api-interactive';
import { showToast, showConfirmDialog } from 'vant';
import { useUserInfoStore } from '@/stores/modules/user-info';
export default () => {
	/**
	 * 点赞收藏
	 * item 当前属性
	 * resourceId 资源id
	 * resourceType 资源类型
	 * operationType 1 浏览 2 帖子点赞 3 收藏 4评论
	 */
	const tyep: any = {
		'1': '',
		'2': 'isLike',
		'3': 'isCollection'
	};
	const getStreamMediaLike = async (item: any, operationType: number, resourceId: any, resourceType: string) => {
		const res: any = await api.getStreamMediaLike({
			clickType: item[tyep[operationType]] ? 1 : 0,
			operationType,
			resourceId,
			resourceType
		});
		if (res.success) {
			switch (operationType) {
				case 2:
					showToast(!item.isLike ? '感谢你的点赞' : '取消成功');
					!item.isLike ? (item.likeNum += 1) : (item.likeNum -= 1);
					item.isLike = !item.isLike;
					break;

				case 3:
					showToast(!item.isCollection ? '感谢你的收藏' : '取消成功');
					!item.isCollection ? (item.collectionNum += 1) : (item.collectionNum -= 1);
					item.isCollection = !item.isCollection;
					break;
				default:
					break;
			}
		}
	};

	// 判断是否登录
	const isPhone = (cd: Function) => {
		const userInfo = useUserInfoStore();
		if (!userInfo.token) {
			showConfirmDialog({
				title: '温馨提示',
				message: '去登录之后才能访问哦~'
			}).then(() => {

			});
		} else {
			cd();
		}
	};
	return {
		getStreamMediaLike,
		isPhone
	};
};
